package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.model.template.AssessmentTemplate;
import com.sduept.bigdata.equipment.assessment.model.template.AssessmentTemplateItem;
import com.sduept.bigdata.equipment.assessment.service.AssessmentTemplateService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.service.equipment.assessment.TableViewModelItemViewService;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 评价模板详情查看
 * @author huangyu
 * @Date 2019年8月7日
 */
@Named
@ViewScoped
public class AssessmentTemplateDetailController extends AbstractController {

	private static final long serialVersionUID = -4413635577225694763L;

	@Autowired
	private AssessmentTemplateService service;
	@Autowired
	private TableViewModelItemViewService viewService;
	
	private String selectedTemplateId;
	
	private TreeNode templateRoot;
	private TreeNode selectedNode;
	private List<TableViewModelItem> templateDetails = new ArrayList<>();
	
	@PostConstruct
	public void init() {
		selectedTemplateId = getParameter("templateId");
		initTemplateTree();
	}
	
	private void initTemplateTree() {
		templateRoot = new LibTreeNode("Root", null);
		// 根据模板id获取模板
		AssessmentTemplate root = service.findById(selectedTemplateId);
		LibTreeNode rootNode = new LibTreeNode("template", root, templateRoot);
		Set<AssessmentTemplateItem> templateItems = service.findAssessmentTemplateItemByAssessmentTemplateId(root.getId());
		for (AssessmentTemplateItem item : templateItems) {
			new LibTreeNode("templateItem", item, rootNode);
		}
		rootNode.setExpanded(true);
	}

	/**
	 * 树节点展开
	 * @param event
	 */
	public void onNodeExpand(NodeExpandEvent event) {
	}
	
	/**
	 * 选择树节点
	 * @param event
	 */
	public void onNodeSelect(NodeSelectEvent event) {
		selectedNode = event.getTreeNode();
		if(selectedNode.getData() instanceof AssessmentTemplateItem) {
			// 选中的是模板项
			AssessmentTemplateItem selectedTemplateItem = (AssessmentTemplateItem) selectedNode.getData();
			templateDetails = viewService.getTableViewModelItemListBytemplate(selectedTemplateItem);
		}
	}
	
	public TreeNode getTemplateRoot() {
		return templateRoot;
	}

	public void setTemplateRoot(TreeNode templateRoot) {
		this.templateRoot = templateRoot;
	}

	public List<TableViewModelItem> getTemplateDetails() {
		return templateDetails;
	}

	public void setTemplateDetails(List<TableViewModelItem> templateDetails) {
		this.templateDetails = templateDetails;
	}
}
